package com.pda.gongpin.ui.fulltrayin;

import android.text.TextUtils;
import android.widget.TextView;

import com.pda.gongpin.R;
import com.pda.gongpin.bean.ScanBean;
import com.pda.gongpin.bean.ScanResultBean;
import com.pda.gongpin.bean.SkuListBean;
import com.pda.gongpin.bean.request.QRcodeRequest;
import com.pda.gongpin.contract.FullTrayInNewContract;
import com.pda.gongpin.dialog.AffirmDialog;
import com.pda.gongpin.net.NetUtils;
import com.pda.gongpin.presenter.FullTrayInNewPresenter;
import com.pda.gongpin.view.ScanButView;
import com.wangshen.base.config.StringKey;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;
import com.wangshen.base.util.SharedPreferencesUtil;
import com.wangshen.base.util.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class FullTrayInThreeFragment extends BaseMvpFragment<FullTrayInNewPresenter> implements FullTrayInNewContract.View {


    @BindView(R.id.tv_materialCode)
    TextView tvMaterialCode;
    @BindView(R.id.tv_articleId)
    TextView tvArticleId;
    @BindView(R.id.tv_articleName)
    TextView tvArticleName;
    @BindView(R.id.tv_spec)
    TextView tvSpec;
    @BindView(R.id.tv_brand)
    TextView tvBrand;
    @BindView(R.id.tv_packageSpecFull)
    TextView tvPackageSpecFull;
    @BindView(R.id.tv_shouldEntryNumStr)
    TextView tvShouldEntryNumStr;
    @BindView(R.id.tv_alreadyEntryNumStr)
    TextView tvAlreadyEntryNumStr;
    @BindView(R.id.et_num)
    TextView etNum;
    @BindView(R.id.tv_pallet)
    TextView tvPallet;
    @BindView(R.id.sb_scan)
    ScanButView sbScan;
    @BindView(R.id.tv_move_out)
    TextView tvMoveOut;
    SkuListBean skuListBean;
    @BindView(R.id.tv_storageBin)
    TextView tvStorageBin;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_standard)
    TextView tvStandard;
    @BindView(R.id.tv_surfaceTreatment)
    TextView tvSurfaceTreatment;
    @BindView(R.id.tv_grade)
    TextView tvGrade;
    private String storageBin;

    public FullTrayInThreeFragment() {
    }

    public FullTrayInThreeFragment(SkuListBean skuListBean) {
        this.skuListBean = skuListBean;
    }

    @Override
    public FullTrayInNewPresenter getPresenter() {
        return new FullTrayInNewPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_full_tray_in_three;
    }

    @Override
    public void initView() {
        storageBin = SharedPreferencesUtil.getInstance().getString(StringKey.STORAGEBIN, "");
        tvStorageBin.setText(storageBin);
        tvTitle.setText(skuListBean.getTitle());
        tvSpec.setText("规格：" + skuListBean.getSpec());
        tvArticleId.setText("物品编码： " + skuListBean.getArticleId());
        tvArticleName.setText("物品名称：" + skuListBean.getArticleName());
        tvAlreadyEntryNumStr.setText("已出库数量：" + skuListBean.getAlreadyEntryNumStr());
        tvBrand.setText("品牌：" + skuListBean.getBrand());
        tvPackageSpecFull.setText("包格：" + skuListBean.getPackageSpecFull());
        tvShouldEntryNumStr.setText("应入库数量：" + skuListBean.getShouldEntryNumStr());
        tvMaterialCode.setText("我的料号" + skuListBean.getMaterialCode());
        tvStandard.setText("标准："+skuListBean.getStandard());
        tvGrade.setText("等级："+skuListBean.getGrade());
        tvSurfaceTreatment.setText("表色："+skuListBean.getSurfaceTreatment());
        etNum.setText(skuListBean.getNum());
    }

    @Override
    public void initLinsenterner() {
        sbScan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                QRcodeRequest request = new QRcodeRequest();
                request.setQRCode(scanBean.getCode());
                getCode(request);
            }
        });
    }

    public void getCode(QRcodeRequest request) {
        NetUtils.geApi().scanTheQRCodeToVerify(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseAppEntity<ScanResultBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ScanResultBean> stringBaseAppEntity) throws Exception {
                        if (!stringBaseAppEntity.getCode().equals("200")) {
                            ToastUtils.showToast(getContext(), stringBaseAppEntity.getMessage());
                            return;
                        }
                        if (stringBaseAppEntity.getData().getType().equals("A")) {
                            tvPallet.setText(stringBaseAppEntity.getData().getCode());
                        } else if (stringBaseAppEntity.getData().getType().equals("B")) {
                            tvStorageBin.setText(stringBaseAppEntity.getData().getCode());
                            storageBin = stringBaseAppEntity.getData().getCode();
                            SharedPreferencesUtil.getInstance().putString(StringKey.STORAGEBIN, stringBaseAppEntity.getData().getCode());
                        } else {
                            ToastUtils.showToast(getContext(), "请扫正确的条形码");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ToastUtils.showToast(getContext(), throwable.getMessage());
                    }
                });
    }

    @Override
    public void getData() {

    }

    @OnClick(R.id.tv_move_out)
    public void onViewClicked() {
        if (TextUtils.isEmpty(tvPallet.getText().toString())) {
            showMessage("托盘不能为空！");
            return;
        }
        if (TextUtils.isEmpty(tvStorageBin.getText().toString())) {
            showMessage("作业库位不能为空！");
            return;
        }
        AffirmDialog affirmDialog = new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
            @Override
            public void onConfrim() {
                presenter.shelves(skuListBean.getEntryNumberId(), tvStorageBin.getText().toString(), tvPallet.getText().toString(), skuListBean.getNum());
            }
        });
        affirmDialog.show("确定开始上架？");
    }

    @Override
    public void setList(List<SkuListBean> list) {

    }
}
